package com.yuou.controller.admin;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuou.annotation.AccessLimit;
import com.yuou.annotation.SystemLog;
import com.yuou.controller.BaseExceptionController;
import com.yuou.model.Bloger;
import com.yuou.model.BlogerRole;
import com.yuou.model.Dept;
import com.yuou.service.BlogerRoleService;
import com.yuou.service.BlogerService;
import com.yuou.service.DeptService;
import com.yuou.util.CipherUtil;
import com.yuou.util.ConstantUtil;


@Controller
@RequestMapping(value = "/admin")
public class BlogerControllerAdmin extends BaseExceptionController {

	
	@Resource(name = "blogerServiceImpl")
	private BlogerService blogerService;
	
	@Resource(name = "deptServiceImpl")
	private DeptService deptService;
	
	@Resource(name = "blogerRoleServiceImpl")
	private BlogerRoleService blogerRoleService;
	
	/**
	  * 修改密码功能
	  * @param resource
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:bloger:edit")
	 @RequestMapping(value = "/updatePassword",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.PASSWORD_MODIFY,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> updatePassWord(String username,String prarm,Integer id,String password) throws Exception{
		 password = CipherUtil.generatePassword(CipherUtil.generatePassword(password+username));
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(blogerService.updatePassword(id, password)!=0){
			 map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 return map;
	 }
	 /**
	  * 验证密码功能
	  * @param resource
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:bloger:bloger")
	 @RequestMapping(value = "/getPassword",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.PASSWORD_CHECK,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> checkPassWord(String username,Integer id,String password) throws Exception{
		 password = CipherUtil.generatePassword(CipherUtil.generatePassword(password+username));
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(blogerService.getPassword(id).equals(password)){
			 map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 return map;
	 }
	 /**
	  * 模糊组合分页查询用户信息(and)
	  * @param blog
	  * @param sort
	  * @param page
	  * @param pageSize
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:bloger:bloger")
	 @RequestMapping(value = "/selectAllblogerWithRoles")
	 @ResponseBody
	 @AccessLimit(seconds=1,maxCount=5)
	 public Map<String, Object> selectAllblogerWithRoles(Bloger bloger,@RequestParam(value="page", required=true,defaultValue="1") Integer page,@RequestParam(value="pageSize", required=true,defaultValue="10") Integer pageSize) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(bloger.getName()!=null&&bloger.getName()!=""){
			 map.put("name", bloger.getName());
		 }
		 if(bloger.getUsername()!=null&&bloger.getUsername()!=""){
			 map.put("username", bloger.getUsername());
		 }
		 if(bloger.getEmail()!=null&&bloger.getEmail()!=""){
			 map.put("email", bloger.getEmail());
		 }
			 map.put("status", bloger.getStatus());
		 if(bloger.getSex()>=0){
			 map.put("sex", bloger.getSex());
		 }
		 if(bloger.getDeptId()>0){
			 map.put("deptId", bloger.getDeptId());
		 }
		 //System.out.println("部门ID========="+bloger.getDeptId());
		 if(bloger.getRoleId()>0){
			 map.put("roleId", bloger.getRoleId());
		 }
		 //分页显示：第1页开始，每页显示10条记录
		 PageHelper.startPage(page, pageSize);
		 List<BlogerRole> blogerList=blogerRoleService.selectAllblogerWithRoles(map);
		 PageInfo<BlogerRole> pageInfo=new PageInfo<BlogerRole>(blogerList);
		 Map<String, Object> returnMap=new HashMap<String, Object>();
		 if(blogerList.size()>0){
			 returnMap.put("status", 200);
		 }else{
			 //0表示：返回值为Null
			 returnMap.put("status", 0);
		 }
		 
		 returnMap.put("blogList", blogerList);
		 returnMap.put("pageInfo", pageInfo);
		 return returnMap;
	 }
	 
	 /**
	  * 按用户角色id查询用户角色信息
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:bloger:bloger")
	 @RequestMapping(value = "/selectBlogerById",method = RequestMethod.POST)
	 @ResponseBody
	 public Map<String, Object> selectBlogerById(String param,Integer id) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 BlogerRole blogerRole=blogerRoleService.selectbloger(id);
		 
		 if(blogerRole!=null){
			 Dept dept= new Dept();
			 String deptName="空";
			 dept =deptService.selectById(blogerRole.getBloger().getDeptId());
			 if(dept!=null) {
				 deptName= dept.getName();
			 }
			 
			 blogerRole.getBloger().setDeptName(deptName);;
			 map.put("status", 200);
			 map.put("blogerRole", blogerRole);
		 }else{
			 //500表示：返回值为Null
			 map.put("status", 500);
		 }
		 
		 return map;
	 }
	 
	 
	 /**
	  * 更新用户功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:bloger:edit")
	 @RequestMapping(value = "/updateBloger",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.BLOGER_MODIFY,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> updateBloger(String prarm,Bloger bloger) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 try {
		 if(bloger.getRoleId()!=0) {
			 //System.out.println("sys:bloger:edit参数"+bloger.getRoleId());
			 blogerRoleService.updateByblogerId(bloger.getId(), bloger.getRoleId());
		 }
		 //System.out.println("imageUrl"+bloger.getHeadImageUrl());
		 //System.out.println(bloger.toString());
		 if(blogerService.updateByPrimaryKeySelective(bloger)!=0){
			 map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 }catch(Exception e) {
			 e.printStackTrace();
		 }
		 return map;
	 }
	 
	 /**
	  * 用户个人修改功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping(value = "/updateBlogerByPerson",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.BLOGER_MODIFY,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> updateBlogerByPerson(String prarm,Bloger bloger) throws Exception{
		 Map<String, Object> map= this.updateBloger(prarm,bloger);
		return map;
	 }
	 
	 /**
	  * 新增用户功能
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequiresPermissions("sys:bloger:add")
	 @RequestMapping(value = "/addBloger",method = RequestMethod.POST)
	 @ResponseBody
	 @SystemLog(description = ConstantUtil.BLOGER_ADD,userType=ConstantUtil.USERTYPE_ADMIN) 
	 public Map<String, Object> addBloger(String prarm,Bloger bloger) throws Exception{
		// System.out.println("sys:bloger:edit参数"+bloger.getRoleId());
		 String password = CipherUtil.generatePassword(CipherUtil.generatePassword(bloger.getPassword()+bloger.getUsername()));
		 bloger.setPassword(password);
		 bloger.setCreateDate(new Date());
		 bloger.setHeadImageUrl("http://www.yuou.fun/upload/background/2018-08-04524082.JPG");
		 //System.out.println(bloger.getName()+bloger.getEmail()+bloger.getStatus());
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(blogerService.insertSelective(bloger)!=0){
			 BlogerRole blogerRole = new BlogerRole();
			 System.out.println(bloger.getId());
			 blogerRole.setBlogerId(bloger.getId());
			 blogerRole.setRoleId(bloger.getRoleId());
			 blogerRoleService.addBlogerIdAndRoleId(blogerRole);
			 map.put("status", 200);
		 }else{
			 //0表示：更新失败
			 map.put("status", 0);
		 }
		 return map;
	 }
	 /**
	  * 验证用户名是否存在
	  * @param blog
	  * @return
	  * @throws Exception
	  */
	 @RequestMapping(value = "/checkuserName",method = RequestMethod.POST)
	 @ResponseBody
	 public Map<String, Object> userNameExist(String prarm,String username) throws Exception{
		 Map<String, Object> map=new HashMap<String, Object>();
		 if(blogerService.getUserByLoginName(username)==null){
			 map.put("status", "200");
		 }else{
			 //已存在
			 map.put("status", "0");
		 }
		 return map;
	 }
	
}
